<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_plugins/caches.php';
require $_SERVER['DOCUMENT_ROOT'].'/../_interface/upload.php';

$db = new DB('star');

$title = isset($_POST['title']) ? h($_POST['title']) : '';
$content = isset($_POST['content']) ? preg_replace('/\n/', '', preg_replace('/\r/', '', $_POST['content'])) : '';
$html = isset($_POST['html']) ? h($_POST['html']) : '';
$views = rand(100,200);
$index_top = isset($_POST['index_top']) ? 2 : 1;
$orders = isset($_POST['orders']) ? intval($_POST['orders']) : 0;
$title_history_id = 1;
$create_uid = Sessions::get('uid');
$edit_uid = $create_uid;
$ip = IP::get();
$is_hide = 1;
$is_del = 1;
$created = time();
$updated = $created;

//检验必要参数
if( $title == '' ){
	echo '<script>alert("姓名不能为空！");window.history.go(-1);</script>';
	exit;
}
if( $content == '' && $html == '' ){
	echo '<script>alert("内容和HTML两项至少要填一项！");window.history.go(-1);</script>';
	exit;
}

//检验标题是否重复
$star_sql = 'select title_id from star where title="'.$title.'" and is_del=1 limit 1';
$star_rs = $db->query($star_sql)->getOne();
if( $star_rs['title_id'] > 0 ){
	echo '<script>alert("发布失败，名人《'.$title.'》已存在！");window.history.go(-1);</script>';
	exit;
}

//上传图片
$up = new Upload($_FILES,'../../../star_upload/');     //创建
$fs = $up->execute();    //开始执行，并返回数据
//名人榜头像
if($_FILES['attach']['size']['star']>0){
	if($fs['star']['flag'] == -2){
		echo '<script>alert("上传名人榜头像大于1M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['star']['flag'] == 1){//上传成功
		$star_images = substr($fs['star']['handle_dir'],8).$fs['star']['name'];
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{//无头像
	$star_images = '/n_upload/handle/20130428/ec0c64df375ed72435b7974acfff0125-1367139431.jpg';
}
//新闻站头像
if($_FILES['attach']['size']['news']>0){
	if($fs['news']['flag'] == -2){
		echo '<script>alert("上传名人榜头像大于1M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['news']['flag'] == 1){//上传成功
		$news_images = substr($fs['news']['handle_dir'],8).$fs['news']['name'];
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{//无头像
	$news_images = '/n_upload/handle/20130608/64ac4a858885861d7fce67eeb3c7056c-1370670701.jpg';
}

//生成当前title_id
$star_max_sql = 'select title_id from star order by title_id desc';
$star_max_rs = $db->query($star_max_sql)->getOne();
$title_id = $star_max_rs['title_id']+1;

//组装静态地址
$static_url = 'http://star.zgqw.com/v/'.$title_id;

$star_data=array('title_id'=>$title_id,
			'title'=>$title,
			'content'=>$content,
			'html'=>$html,
			'index_thumb'=>$news_images,
			'thumb'=>$star_images,
			'static_url'=>$static_url,
			'views'=>$views,
			'index_top'=>$index_top,
			'orders'=>$orders,
			'title_history_id'=>$title_history_id,
			'create_uid'=>$create_uid,
			'edit_uid'=>$edit_uid,
			'ip'=>$ip,
			'is_hide'=>$is_hide,
			'is_del'=>$is_del,
			'created'=>$created,
			'updated'=>$updated,
			);
$db->table('star')->insert($star_data)->run();//插入

//组装history的insert数组
$history_data=array('history_id'=>$title_history_id,
			'title_id'=>$title_id,
			'title'=>$title,
			'content'=>$content,
			'html'=>$html,
			'index_thumb'=>$news_images,
			'thumb'=>$star_images,
			'static_url'=>$static_url,
			'views'=>$views,
			'create_uid'=>$create_uid,
			'created'=>$created,
			);
$db->table('star_history')->insert($history_data)->run();//插入

//组装track的insert数组
$track_data=array('track_id'=>1,
			'title_history_id'=>$title_history_id,
			'title_id'=>$title_id,
			'create_uid'=>$create_uid,
			'created'=>$created,
			'changes'=>'0-1',
			);
$db->table('star_track')->insert($track_data)->run();//插入

//生成index.html
$filepath = '/v/'.$title_id;
$filename = '/index.html';
if( $html=='' ){
	$url = 'http://star.zgqw.com/show.php?title_id='.$title_id;
	Caches::toStatic( $url, 'www'.$filepath.$filename );
}else{
	Caches::mkdirs( $_SERVER['DOCUMENT_ROOT'].$filepath );
	@file_put_contents( $_SERVER['DOCUMENT_ROOT'].$filepath.$filename, h_d($html) );
}

//上传附件
if($_FILES['attachment']['size']['attachment']>0){
	$up = new Upload($_FILES, '../../../../_cache/_webfiles/vip/'.$title_id.'/', 'zip', 10240, 'attachment');     //创建
	$fs = $up->execute();    //开始执行，并返回数据
	
	if($fs['attachment']['flag'] == -2){
		echo '<script>alert("上传附件大于10M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['attachment']['flag'] == 1){//上传成功
		$attachment = substr($fs['attachment']['handle_dir'],8).$fs['attachment']['name'];
	}else{
		echo '<script>alert("上传附件出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
	
	//解压
	$zip = new ZipArchive;
	if ($zip->open( $_SERVER['DOCUMENT_ROOT'].$attachment ) === TRUE) {
		$zip->extractTo( $_SERVER['DOCUMENT_ROOT'].'/v/'.$title_id.'/' );
		$zip->close();
	}else{
		echo '<script>alert("附件格式出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}

//获取文件内容写入数据库(压缩包里可能有index.html)
$file_content = h( @file_get_contents( $_SERVER['DOCUMENT_ROOT'].$filepath.$filename ) );
$update_data = array('html'=>$file_content);
$db->table('star')->where('title_id='.$title_id)->update($update_data)->run();//更新
$db->table('star_history')->where('title_id='.$title_id)->update($update_data)->run();//更新

echo '<script>alert("添加成功！");window.location.href="/www/admin/star/star_lists.php";</script>';
exit;